package com.ruoyi.web.controller.system;


import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.app.*;
import com.ruoyi.system.service.CommunityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "小区相关")
@RestController
@RequestMapping()
public class CommunityController extends BaseController {

    @Autowired
    CommunityService communityService;



    @ApiOperation(value = "根据运营商名称获取运营商列表",notes = "system:operator:list")
    @PreAuthorize("@ss.hasPermi('system:operator:list')")
    @GetMapping("/operator/list")
    public AjaxResult getOperatorByName(@RequestParam String operatorName){
        return AjaxResult.success(communityService.getOperatorByName(operatorName));
    }

    @ApiOperation(value = "根据分账人名称获取分账人",notes = "system:partner:list")
    @PreAuthorize("@ss.hasPermi('system:partner:list')")
    @GetMapping("/partner/list")
    public AjaxResult getPartnerByName(@RequestParam String partnerName){
        return AjaxResult.success(communityService.getPartnerByName(partnerName));
    }

    @ApiOperation(value = "获取小区下分账人列表",notes = "system:community:partner:list")
    @PreAuthorize("@ss.hasPermi('system:community:partner:list')")
    @GetMapping("/community/partner/list")
    public AjaxResult getCommunityPartnerList(@RequestParam Long communityId){
        return AjaxResult.success(communityService.getCommunityPartnerList(communityId));
    }

    @ApiOperation(value = "小区新增分账人",notes = "system:partner:add")
    @PreAuthorize("@ss.hasPermi('system:partner:add')")
    @PostMapping("/add/partner")

    public AjaxResult addPartner(@RequestParam Long communityId,@RequestParam Long userId,@RequestParam Double ratio){
        communityService.addPartner(communityId,userId,ratio);
        return AjaxResult.success();
    }

    @ApiOperation(value = "删除分账人",notes = "system:partner:del")
    @PreAuthorize("@ss.hasPermi('system:partner:del')")
    @PostMapping("/del/partner")
    public AjaxResult delPartner(@RequestParam Long partnerId){
        communityService.delPartner(partnerId);
        return AjaxResult.success();
    }

    @ApiOperation(value = "修改分账人比例",notes = "system:partner:set")
    @PreAuthorize("@ss.hasPermi('system:partner:set')")
    @PostMapping("/set/partner")
    public AjaxResult setPartner(@RequestParam Long communityId,@RequestParam Long partnerId,@RequestParam Double ratio){
        communityService.setPartner(communityId,partnerId,ratio);
        return AjaxResult.success();
    }

    @ApiOperation(value = "获取注册会员列表",notes = "system:register:user:list")
    @PreAuthorize("@ss.hasPermi('system:register:user:list')")
    @PostMapping("/register/user/list")
    public AjaxResult registerUserList(String userName,String phone){
        startPage();
        return AjaxResult.success(getDataTable(communityService.registerUserList(userName,phone)));
    }

    @ApiOperation(value = "会员删除",notes = "system:register:user:del")
    @PreAuthorize("@ss.hasPermi('system:register:user:del')")
    @DeleteMapping("/register/user/del")
    public AjaxResult registerUserDel(@RequestParam Long userId){
        communityService.registerUserDel(userId);
        return AjaxResult.success();
    }

    @ApiOperation(value = "会员启用禁用",notes = "system:user:state  1启用 2禁用")
    @PreAuthorize("@ss.hasPermi('system:user:state')")
    @DeleteMapping("/user/state")
    public AjaxResult userState(@RequestParam Long userId,@RequestParam Integer state){
        communityService.userState(userId,state);
        return AjaxResult.success();
    }

    @ApiOperation(value = "修改用户手机号",notes = "system:set:user:phone")
    @PreAuthorize("@ss.hasPermi('system:set:user:phone')")
    @PutMapping("/set/user/phone")
    public AjaxResult setUserPhone(@RequestParam Long userId,@RequestParam String phone){
        communityService.setUserPhone(userId,phone);
        return AjaxResult.success();
    }



    @ApiOperation(value = "获取小区会员列表",notes = "system:user:list")
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @PostMapping("/user/list")
    public AjaxResult userList(@RequestParam Long communityId,String userName,String phone){
        startPage();
        return AjaxResult.success(getDataTable(communityService.userList(communityId,userName,phone)));
    }

    @ApiOperation(value = "会员手动充值",notes = "system:user:charge type 1钱包金额 2包月时长 value在type = 1为元 type = 2 为天")
    @PreAuthorize("@ss.hasPermi('system:user:charge')")
    @PostMapping("/user/charge")
    public AjaxResult userCharge(@RequestParam Long communityId,@RequestParam Long userId,@RequestParam Integer type,@RequestParam String value){
        communityService.userCharge(communityId,userId,type,value);
        return AjaxResult.success();
    }

    @ApiOperation("手动用户加入小区")
    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @PostMapping("/user/add")
    public AjaxResult userAddCommunity(@RequestParam String phone,@RequestParam Long communityId){
        communityService.userAddCommunity(phone,communityId);
        return AjaxResult.success();
    }

    @ApiOperation(value = "添加小区",notes = "system:community:add")
    @PreAuthorize("@ss.hasPermi('system:community:add')")
    @PostMapping("/add/community")
    public AjaxResult addCommunity(@RequestParam String communityName,String longitude,String latitude,
                                   Long operatorId,String operatorName,String site){
        Community community = new Community();
        community.setCommunityName(communityName);
        community.setLatitude(latitude);
        community.setLongitude(longitude);
        community.setOperatorId(operatorId);
        community.setOperatorName(operatorName);
        community.setSite(site);
        communityService.addCommunity(community);
        return AjaxResult.success();
    }

    @ApiOperation(value = "删除小区",notes = "system:community:del")
    @PreAuthorize("@ss.hasPermi('system:community:del')")
    @DeleteMapping("/del/community")
    public AjaxResult delCommunity(@RequestParam Long communityId){
        communityService.delCommunity(communityId);
        return AjaxResult.success();
    }

    @ApiOperation(value = "修改小区",notes = "system:community:set")
    @PreAuthorize("@ss.hasPermi('system:community:set')")
    @PutMapping("/set/community")
    public AjaxResult setCommunity(@RequestParam Long communityId,@RequestParam String communityName,String latitude,String longitude,
                                   Integer state,Integer isBalanceUse,String site,String operatorName,Long operatorId){
        Community community = new Community();
        community.setId(communityId);
        community.setCommunityName(communityName);
        community.setLatitude(latitude);
        community.setOperatorId(operatorId);
        community.setOperatorName(operatorName);
        community.setLongitude(longitude);
        community.setCommunityState(state);
        community.setIsBalanceUse(isBalanceUse);
        community.setSite(site);
        communityService.setCommunity(community);
        return AjaxResult.success();
    }

    @ApiOperation("混合查询小区,返回符合条件的小区id集合")
    @PreAuthorize("@ss.hasPermi('system:community:list')")
    @GetMapping("/community/list/ids")
    public AjaxResult communityListIds(String phone,String cardNum,String deviceNum){
        return AjaxResult.success(communityService.communityListIds(phone,cardNum,deviceNum));
    }

    @ApiOperation(value = "小区列表",notes = "system:community:list")
    @PreAuthorize("@ss.hasPermi('system:community:list')")
    @GetMapping("/community/list")
    public AjaxResult communityList(String communityName,@RequestParam(required = false) List<Long> communityIds){
        startPage();
        Community community = new Community();
        community.setCommunityName(communityName);
        return AjaxResult.success(getDataTable(communityService.communityList(community,communityIds)));
    }

    @ApiOperation(value = "重置：钱包充值，电卡充值，包月充值 时间段配置",notes = "system:option:reset  传json数组 钱包：key：charge 电卡 cardCharge 包月 combo 时间段 time 临时充电 recharge")
    @PreAuthorize("@ss.hasPermi('system:option:reset')")
    @PostMapping("/option/reset")
    public AjaxResult optionReset(@RequestBody JSONObject jsonObject){
        communityService.optionReset(jsonObject);
        return AjaxResult.success();
    }

    @ApiOperation(value = "获取：钱包充值，电卡充值，包月充值",notes = "system:option:get 钱包：key：charge 电卡 cardCharge 包月 combo  临时充电 recharge")
    @PreAuthorize("@ss.hasPermi('system:option:get')")
    @PostMapping("/option/get")
    public AjaxResult optionGet(@RequestParam Long communityId){
        return AjaxResult.success(communityService.optionGet(communityId));
    }


    @ApiOperation(value = "获取小区钱包充值配置",notes = "system:charge:list")
    @PreAuthorize("@ss.hasPermi('system:charge:list')")
    @GetMapping("/charge/list")
    public AjaxResult getChargeList(@RequestParam Long communityId){

        return AjaxResult.success(communityService.getChargeList(communityId));
    }

    @ApiOperation(value = "重置小区钱包充值配置",notes = "system:charge:reset")
    @PreAuthorize("@ss.hasPermi('system:charge:reset')")
    @PostMapping("/reset/charge")
    public AjaxResult resetCharge(@RequestBody List<ChargeOption> chargeOptions){
        communityService.resetCharge(chargeOptions);
        return AjaxResult.success();
    }

    @ApiOperation(value = "获取小区电卡充值配置",notes = "system:card:charge:list")
    @PreAuthorize("@ss.hasPermi('system:card:charge:list')")
    @GetMapping("/card/charge/list")
    public AjaxResult getCardChargeList(@RequestParam Long communityId){
        return AjaxResult.success(communityService.getCardChargeList(communityId));
    }

    @ApiOperation(value = "重置小区电卡充值配置",notes = "system:card:charge:reset")
    @PreAuthorize("@ss.hasPermi('system:card:charge:reset')")
    @PostMapping("/reset/card/charge")
    public AjaxResult resetCardCharge(@RequestBody List<CardChargeOption> cardChargeOptions){
        communityService.resetCardCharge(cardChargeOptions);
        return AjaxResult.success();
    }

    @ApiOperation(value = "获取小区包月充值配置",notes = "system:combo:list")
    @PreAuthorize("@ss.hasPermi('system:combo:list')")
    @GetMapping("/combo/list")
    public AjaxResult getComboList(@RequestParam Long communityId){
        return AjaxResult.success(communityService.getComboList(communityId));
    }

    @ApiOperation(value = "重置小区包月充值配置",notes = "system:combo:reset")
    @PreAuthorize("@ss.hasPermi('system:combo:reset')")
    @PostMapping("/reset/combo")
    public AjaxResult resetCombo(@RequestBody List<Combo> combos){
        communityService.resetCombo(combos);
        return AjaxResult.success();
    }

    @ApiOperation(value = "获取小区充电时间选项",notes = "system:time:option")
    @PreAuthorize("@ss.hasPermi('system:time:option')")
    @GetMapping("/time/option")
    public AjaxResult getTimeOption(@RequestParam Long communityId){
        return AjaxResult.success(communityService.getTimeOption(communityId));
    }

    @ApiOperation(value = "重置小区充电时间选项",notes = "system:time:reset")
    @PreAuthorize("@ss.hasPermi('system:time:reset')")
    @PostMapping("/time/reset")
    public AjaxResult resetTimeOption(@RequestBody List<ConsumptionOption> consumptionOptions){
        communityService.resetTimeOption(consumptionOptions);
        return AjaxResult.success();
    }



}
